﻿using System;
using System.Collections.Generic;
using System.Text;

/*
 * Author:Conis
 * Date:
 * Description:JSON的数组子节点
 */

namespace Coins.Squirrel.Nodous.JSON
{
    public class JSONNodousItemNode : JSONNodousComponent, INodousNode
    {
        #region 普通方法
        /// <summary>
        /// 创建数组子节点
        /// </summary>
        protected internal JSONNodousItemNode()
            : base(string.Empty, string.Empty) { }

        /// <summary>
        /// 创建前缀
        /// </summary>
        protected override void CreatePreifx()
        {
            this.container.Append("{");
        }

        /// <summary>
        /// 创建生缀
        /// </summary>
        protected override void CreateSuffix()
        {
            this.container.Append("}");
            //如果当前节点不是最后一个节点，则加上逗号
            if (this.currentIndex != this.brotherCount)
            {
                this.container.Append(",");
            }
        }
        #endregion

        #region INodousNode 成员
        /// <summary>
        /// 创建数组节点
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <returns></returns>
        public INodousItem AddItem(string name)
        {
            JSONNodousItem item = new JSONNodousItem(name);
            this.Add(item);
            item.Parent = this;
            return item;
        }

        /// <summary>
        /// 创建普通节点
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <returns></returns>
        public INodousNode AddNode(string name)
        {
            JSONNodousNode node = new JSONNodousNode(name);
            this.Add(node);
            node.Parent = this;
            return node;
        }

        /// <summary>
        /// 创建叶子节点
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <param name="value">节点值</param>
        /// <returns></returns>
        public INodousLeaf AddLeaf(string name, string value)
        {
            JSONNodousLeaf leaf = new JSONNodousLeaf(name, value);
            this.Add(leaf);
            leaf.Parent = this;
            return leaf;
        }

        /// <summary>
        /// 创建一个安全的叶子节点，功能与AddLeaf相同
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public INodousLeaf SafeAddLeaf(string name, string value)
        {
            return this.AddLeaf(name, value);
        }

        #endregion
    }
}
